<?php
// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------

namespace app\customer\admin;
use app\admin\admin\Base;
use service\Format;


/**
 * 门店管理
 * @package app\customer\admin
 */
class Shop extends Base
{
    /**
     * 门店列表
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
        $map =[];
        $param = $this->request->param();    
        if ($param["store_id"]) {
            $map[] = ["store_id","=",$param["store_id"]];
        }    
        if ($param["store_name"]) {
            $map[] = ["store_name","like",'%' . $param["store_name"] . '%'];
        }     
        // 排序
        $order = $this->getOrder('id desc');

        $data_list = \app\customer\model\Shop::getList($map,$order);
   
        $fields = [
           // ['id', 'ID'],
            ['store_id', '店铺ID', 'text'],
            ['store_name', '名称'],
            ['store_logo', 'LOGO', 'picture'],
            ['create_time', '创建时间', '','','','text-center'],

            ["service_date","营业时间"],
            ["service_phone","客服电话"],
            ['is_robot', '自动回复', 'switch'],
            ["greeting","问候语"],
            //['status', '状态', 'status'],
            ['right_button', '操作', 'btn', '', '', 'text-center']
        ];
        $search_fields = [
            ['store_id', '店铺ID', 'text'],
            ['store_name', '店铺名称', 'text'],
        ];
        return Format::ins()//实例化
         
            ->setTopSearch($search_fields)
            //->setTopButton(['title'=>'注册店铺','href'=>['add'],'icon'=>'fa fa-plus pr5','class'=>'btn btn-sm mr5 btn-primary btn-flat layeradd'])          

            ->setRightButtons($this->right_button_layer,['delete',"enable","disable"])  
           ->setRightButtons([
               ['ident'=> 'cust', 'title'=>'客服','href'=>['customer/customer/index',['store_id'=>'__store_id__']],'class'=>'btn btn-xs mr5'],
            ])
            ->addColumns($fields)//设置字段   
            ->setTableName("customer_shop")
            ->setPrimaryKey("id")
            ->setData($data_list)  
            ->fetch();
    }

    /**
     * 新增
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();    
            $res = $this->validate($data, "shop");
            if(true !== $res){
                $this->error($res);
            }
            if(!$data["store_id"]){
                $this->error("请填写店铺ID");
            }
            //注册客服         
            \think\Db::startTrans();
            try{    
                \app\customer\model\Shop::register($data['store_id'], $data);
                \think\Db::commit();            
            }catch(\Exception $e){
                \think\Db::rollback();          
                $this->error( $e->getMessage());
            }              
            $this->success('店铺注册成功', cookie('__forward__'));          
        }   
   
        $fields = [           
            ['type' => 'number', 'name' => 'store_id', 'title' => '店铺ID'],            
            ['type' => 'text', 'name' => 'store_name', 'title' => '店铺名称'],
            ['type' => 'image', 'name' => 'store_logo', 'title' => '店铺LOGO'],
            ['type' => 'text', 'name' => 'greeting', 'title' => '问候语'],
            ['type' => 'text', 'name' => 'service_date', 'title' => '营业时间',"tips"=>"如9:00-18:00"],
            ['type' => 'text', 'name' => 'service_phone', 'title' => '客服电话'],
          
        ];
        $this->assign('page_title', '新增店铺');
        $this->assign('form_items', $fields);
        return $this->fetch('admin@public/add');
    }
     /**
     * 编辑
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function edit($id = null)
    {
        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();                  
            $res = $this->validate($data, "shop");
            if(true !== $res){
                $this->error($res);
            }
             $data["store_id"] = null;
             unset( $data["store_id"]);    
             \app\customer\model\Shop::update($data);                         
             $this->success('编辑成功', cookie('__forward__'));          
        }   
        $info = \app\customer\model\Shop::get($id);
        $fields = [           
            ['type' => 'hidden', 'name' => 'id'],
            ['type' => 'text', 'name' => 'store_name', 'title' => '店铺名称'],
            ['type' => 'image', 'name' => 'store_logo', 'title' => '店铺LOGO'],
            ['type' => 'text', 'name' => 'greeting', 'title' => '问候语'],
            ['type' => 'text', 'name' => 'service_date', 'title' => '营业时间',"tips"=>"如9:00-18:00"],
            ['type' => 'text', 'name' => 'service_phone', 'title' => '客服电话'],
          
        ];
        $this->assign('page_title', '编辑店铺');
        $this->assign('form_items', $this->setData($fields,$info));
        return $this->fetch('admin@public/edit');
    }
     /**
     * 删除
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function delete($ids = null){
        if(!$ids){
            $this->error("缺少参数");
        }
        $info = \app\customer\model\Shop::where("id",$ids)->find();
        if(!$info){
            $this->error("数据异常");
        }
        \app\customer\model\Shop::delInfo($info['store_id']);
        $this->success("删除成功");
    }
  
}